package cn.edu.jsu.zjj.service.impl;

import cn.edu.jsu.zjj.constant.CommonConstant;
import cn.edu.jsu.zjj.exception.BaseException;
import cn.edu.jsu.zjj.mapper.MajorMapper;
import cn.edu.jsu.zjj.model.dto.major.MajorQueryRequest;
import cn.edu.jsu.zjj.model.entity.Major;
import cn.edu.jsu.zjj.model.entity.TreeSelect;
import cn.edu.jsu.zjj.model.utils.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.jsu.zjj.service.MajorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ly
* @description 针对表【sys_major(系统专业设置)】的数据库操作Service实现
* @createDate 2024-11-09 10:17:12
*/
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major>
    implements MajorService {
    @Autowired
    private MajorMapper majorMapper;

    @Override
    public List<TreeSelect> selectDeptTreeList(Major major) {
        List<Major> majors = this.list();
        List<Major> deptTrees = buildDeptTree(majors);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private List<Major> buildDeptTree(List<Major> majors) {
        List<Major> returnList = new ArrayList();
        List<Long> temList = majors.stream().map(Major::getId).collect(Collectors.toList());
        for (Major major : majors) {
            if (!temList.contains(major.getParentId())) {
                recursion(majors, major);
                returnList.add(major);
            }
        }
        if (returnList.isEmpty()) {
            returnList = majors;
        }
        return returnList;
    }

    private void recursion(List<Major> majors, Major major) {
        List<Major> childList = getChildList(majors, major);
        major.setChildren(childList);
        for (Major tChild : childList) {
            if (hasChild(majors, tChild)) {
                recursion(majors, tChild);
            }
        }
    }

    private List<Major> getChildList(List<Major> list, Major t) {
        List<Major> tlist = new ArrayList<Major>();
        Iterator<Major> it = list.iterator();
        while (it.hasNext()) {
            Major n = it.next();
            if (cn.edu.jsu.zjj.model.utils.StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<Major> list, Major t) {
        return getChildList(list, t).size() > 0;
    }

    @Override
    public QueryWrapper<Major> getQueryWrapper(MajorQueryRequest majorQueryRequest) {
        if (majorQueryRequest == null) {
            throw new BaseException("请求参数为空");
        }
        Long id = majorQueryRequest.getId();
        String name = majorQueryRequest.getName();
        Integer type = majorQueryRequest.getType();
        String treePath = majorQueryRequest.getTreePath();
        String sortField = majorQueryRequest.getSortField();
        String sortOrder = majorQueryRequest.getSortOrder();
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(type != null, "type", type);
        queryWrapper.eq(StringUtils.isNotBlank(treePath), "treePath", treePath);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }
}




